home *** CD-ROM | disk | FTP | other *** search
- # include "defs.h"
- # include <FCntl.h>
- # include <CType.h>
-
- static char *cur_input_file = ""; /* For use by findinc() */
-
- /*
- * Highest-level routine for tracking down #include files
- * and generating a list of dependencies. Calls includes()
- * with the given filename as a file to read, the corresponding
- * object filename as the dependent, and the output FILE pointer.
- */
- depend (out, file)
- FILE *out; /* Where the output should go */
- char *file; /* File to be read */
- {
- int linelen = 0; /* Number of chars on current output line */
- extern char *obj ();
-
- includes (out, obj (file), file, 0, &linelen);
- }
-
- /*
- * Open "file" and look for #include lines. Follow them up
- * recursively, and output dependency statements as you go.
- */
- includes (out, orig, file, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- char *file; /* File to open and read */
- int num; /* Number of includes output */
- int *linelen; /* Number of chars on current output line */
- {
- FILE *f; /* For reading the file */
- char *sfx; /* Filename suffix */
- char *save_cur; /* For saving and restoring cur_input_file */
- char curfile[LINELEN]; /* For storing cur_input_file */
- extern char *strrchr(); /* Routine for obtaining file suffixes */
-
-
- progress ("Reading \"%s\"…\n", file);
-
- /*
- * Get the suffix
- */
- sfx = strrchr (file, '.');
-
-
- /*
- * Open the file for reading
- */
- if ((f = fopen (file, "r")) == NULL)
- {
- OSgoof ("Can't open \"%s\" for reading.\n", file);
- return;
- }
-
- /*
- * Set cur_input_file -- findinc() will use this file's directory
- * prefix (if necessary and if any) to look for any files that this
- * file includes.
- */
- strcpy (curfile, file);
- save_cur = cur_input_file;
- cur_input_file = curfile;
-
- /*
- * Call the appropriate routine to read and parse the file.
- */
- if (strcmp (sfx, ".c") == 0 || strcmp (sfx, ".h") == 0)
- CInc (out, orig, f, num, linelen);
- else if (strcmp (sfx, ".p") == 0)
- PInc (out, orig, f, num, linelen);
- else if (strcmp (sfx, ".r") == 0)
- RInc (out, orig, f, num, linelen);
- else if (strcmp (sfx, ".a") == 0)
- AInc (out, orig, f, num, linelen);
-
- cur_input_file = save_cur; /* Restore previous value */
- fclose (f);
- }
-
- AInc (out, orig, f, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- FILE *f; /* For reading the file */
- int num; /* Number of includes already output */
- int *linelen; /* Number of chars on current output line */
- {
- char buf[LINELEN]; /* For input lines from the file */
- char *argl[ARGMAX]; /* Holds words from an input line */
- int count; /* Number of words in an input line */
- int incix; /* Index into argl[] */
- char *inc; /* Pointer to new filename */
- char *ninc; /* Another pointer to new filename */
- bool is_label; /* TRUE if first char of line isn't blank*/
- extern char *findinc(); /* Routine to search for files */
-
- /*
- * Read each line of the file
- */
- while (fgets (buf, LINELEN, f) != NULL)
- {
- /* First look for position sensitive label field */
- if (!isspace (buf[0]))
- is_label = TRUE;
- else
- is_label = FALSE;
-
- /*
- * Break the line into space-delimited words
- * and look for #include directives
- */
- count = vector (ARGMAX, argl, buf);
-
- if (argl[0][0] == '*' || argl[0][0] == ';')
- continue; /* This is a comment line */
-
- /*
- * If there is a label, argl[1] is the directive;
- * else argl[0] is.
- */
- if (is_label || argl[0][ strlen (argl[0])-1 ] == ':')
- incix = 1;
- else
- incix = 0;
- if (strcmp (argl[incix], "INCLUDE") != 0)
- continue;
- incix++; /* Filename follows the directive */
-
- /*
- * Found an INCLUDE line; argl[incix] is the word
- * containing the filename. Let
- * inc point to the filename, and trim off surrounding quotes
- * if any.
- */
- inc = argl[incix];
- if (*inc == '\'' && inc[ strlen (inc) - 1] == '\'')
- {
- inc++;
- inc[ strlen (inc) - 1] = EOS;
- }
-
- /*
- * File may be local. If found, print a dependency line, and
- * recursively examine it for more includes.
- */
- if (exists (inc))
- {
- prdepend (out, orig, inc, num, linelen);
- num++;
- includes (out, orig, inc, num, linelen);
- }
- else
- {
- /*
- * File is not local. Call findinc() to locate it,
- * and if found, print a dependency line and
- * recursively examine it.
- */
- ninc = findinc (inc, adirlist);
- if (ninc == NULL)
- goof ("\"%s\": file not found\n", inc);
- else
- {
- prdepend (out, orig, ninc, num, linelen);
- num++;
- includes (out, orig, ninc, num, linelen);
- }
- }
-
- /*
- * All done with this line; loop back for another.
- */
- }
-
- /*
- * All done with this file.
- */
- }
-
- CInc (out, orig, f, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- FILE *f; /* For reading the file */
- int num; /* Number of includes already output */
- int *linelen; /* Number of chars on current output line */
- {
- char buf[LINELEN]; /* For input lines from the file */
- char *argl[ARGMAX]; /* Holds words from an input line */
- int count; /* Number of words in an input line */
- int incix; /* Index into argl[] */
- char *inc; /* Pointer to new filename */
- char *delim; /* Pointer to filename delimiter */
- extern char *findinc(); /* Routine to search for files */
-
- /*
- * Read each line of the file
- */
- while (fgets (buf, LINELEN, f) != NULL)
- {
- /*
- * Break the line into space-delimited words
- * and look for #include directives
- */
- count = vector (ARGMAX, argl, buf);
- if (count < 2)
- continue;
- if (strcmp (argl[0], "#include") == 0)
- incix = 1;
- else if (strcmp (argl[0], "#") == 0
- && strcmp (argl[1], "include") == 0)
- incix = 2;
- else
- continue;
- if (incix >= count)
- continue;
-
- /*
- * Found a #include line; argl[incix] is the word
- * containing the filename, either in <brackets>
- * or in "quotes". Record which delimiter it is, and let
- * inc point to the filename stripped of delimiters.
- */
- delim = argl[incix];
- inc = delim + 1;
- inc[ strlen (inc) - 1 ] = EOS;
-
- if (*delim == '"')
- {
- /*
- * File may be local. If found, print a dependency line,
- * add it to the list of local include files, and
- * recursively examine it for more #includes.
- *
- * If it is not found locally, pretend that the delimiter
- * was a '<' and move on.
- */
- if (exists (inc))
- {
- prdepend (out, orig, inc, num, linelen);
- add_dot_h (inc);
- num++;
- includes (out, orig, inc, num, linelen);
- }
- else
- *delim = '<'; /* And fall through */
- }
-
- /*
- * File is not local. Call findinc() to locate it,
- * and if found, print a dependency line and
- * recursively examine it.
- */
- if (*delim == '<')
- {
- inc = findinc (inc, cdirlist);
- if (inc == NULL)
- goof ("%s>: file not found\n", argl[incix]);
- else
- {
- prdepend (out, orig, inc, num, linelen);
- num++;
- includes (out, orig, inc, num, linelen);
- }
- }
-
- /*
- * All done with this line; loop back for another.
- */
- }
-
- /*
- * All done with this file.
- */
- }
-
- PInc (out, orig, f, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- FILE *f; /* For reading the file */
- int num; /* Number of includes already output */
- int *linelen; /* Number of chars on current output line */
- {
- bool in_uses = FALSE; /* Stackable data for Pparse() */
- bool dollar_u = FALSE; /* Stackable data for Pparse() */
- char *inc; /* Pointer to new filename */
- char *ninc; /* Another pointer to new filename */
- extern char *findinc(); /* Routine to search for files */
- extern char *strchr (); /* Routine to search a string for a char*/
- extern char *Pparse ();
-
- while ((inc = Pparse (&in_uses, &dollar_u, f)) != NULL)
- {
- /*
- * Found a filename.
- * File may be local. If found, print a dependency line, and
- * recursively examine it for more includes.
- *
- * If it is not found locally, try findinc().
- */
- if (exists (inc))
- {
- prdepend (out, orig, inc, num, linelen);
- num++;
- includes (out, orig, inc, num, linelen);
- }
- else
- {
- /*
- * File is not local. Call findinc() to locate it,
- * and if found, print a dependency line and
- * recursively examine it. (Check first to see if it's
- * a relative pathname.)
- */
- if (*inc == ':' || strchr (inc, ':') == NULL)
- ninc = findinc (inc, pdirlist);
- else
- ninc = NULL;
-
- if (ninc == NULL)
- goof ("\"%s\": file not found\n", inc);
- else
- {
- prdepend (out, orig, ninc, num, linelen);
- num++;
- includes (out, orig, ninc, num, linelen);
- }
- }
- }
- }
-
- RInc (out, orig, f, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- FILE *f; /* For reading the file */
- int num; /* Number of includes already output */
- int *linelen; /* Number of chars on current output line */
- {
- char *inc; /* Pointer to new filename */
- char *ninc; /* Another pointer to new filename */
- extern char *findinc(); /* Routine to search for files */
- extern char *strchr (); /* Routine to search a string for a char*/
- extern char *Rparse (); /* Routine to parse Rez input files */
-
- /*
- * Read each line of the file
- */
- while ((inc = Rparse (f)) != NULL)
- {
- /*
- * Found an INCLUDE or READ line.
- * File may be local. If found, print a dependency line.
- *
- * If it is not found locally, try findinc().
- */
- if (exists (inc))
- prdepend (out, orig, inc, num, linelen);
- else
- {
- /*
- * File is not local. Call findinc() to locate it,
- * and if found, print a dependency line.
- */
- if (*inc == ':' || strchr (inc, ':') == NULL)
- ninc = findinc (inc, rdirlist);
- else
- ninc = NULL;
-
- if (ninc == NULL)
- goof ("\"%s\": file not found\n", inc);
- else
- prdepend (out, orig, ninc, num, linelen);
- }
-
- /*
- * All done with this line; loop back for another.
- */
- }
-
- /*
- * All done with this file.
- */
- }
-
- /*
- * Search for the given filename in the list of include directories.
- * Return a pointer to the full pathname, or NULL if not found.
- */
- char *findinc (file, list)
- char *file; /* Filename */
- char **list[]; /* List of include directories */
- {
- int i; /* Loop counter */
- char *p; /* Temp pointer */
- int flen = strlen (file); /* Length of filename */
- static char buf[LINELEN]; /* Buffer for pathname */
- extern bool exists ();
-
- /*
- * Skip a leading colon, if any.
- */
- if (*file == ':')
- {
- file++;
- flen--;
- }
-
- /*
- * Check in the directory of the current input file.
- */
- strcpy (buf, cur_input_file);
- if ((p = strrchr (buf, ':')) != NULL)
- {
- p++;
- *p = EOS;
- if (strlen (buf) > LINELEN - (flen + 1))
- {
- goof ("Search path too long: \"%s%s\"\n",
- buf, file);
- return (NULL);
- }
- strcat (buf, file);
- if (exists (buf))
- return (buf);
- }
-
- /*
- * For each search directory, construct a pathname and see if the
- * file is there. Return the path if so.
- */
- for (i = 0; i < DIRMAX && list[i] != NULL; i++)
- {
- if (strlen (list[i]) > LINELEN - (flen + 1))
- {
- goof ("Search path too long: \"%s%s\"\n",
- list[i], file);
- return (NULL);
- }
- strcpy (buf, list[i]);
- strcat (buf, file);
- if (exists (buf))
- return (buf);
- }
-
- /*
- * Nowhere to be found.
- */
- return (NULL);
- }
-
- prdepend (out, orig, inc, num, linelen)
- FILE *out; /* Where the output should go */
- char *orig; /* The original "dependent" file */
- char *inc; /* The dependency (included) file */
- int num; /* Number of includes output */
- int *linelen; /* Number of chars on current output line */
- {
- # define PRETTYLINELEN 50
- if (num == 0)
- {
- fprintf (out, "%s ƒ %s", orig, inc);
- *linelen = strlen (orig) + strlen (inc) + 3;
- }
- else
- {
- if (*linelen > PRETTYLINELEN)
- {
- fprintf (out, " ∂\n\t");
- *linelen = 4; /* For the tab */
- }
- fprintf (out, " %s", inc);
- *linelen += strlen (inc) + 1;
- }
- }
-